import json
from datetime import datetime

from django.core.paginator import Paginator
from django.views import View
from django.http import JsonResponse
from lapi_app.models.testcase_model.testcase import Testcase
from lapi_app.models.testcase_model.testcase_tree import Menu


class Testcase_list(View):
    def post(self, request, *args, **kwargs):
        """
        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        path = request.get_full_path()
        if path == "/api/testcase/list":
            data = json.loads(request.body)
            pid = data["id"]
            size_page = data["size_page"]
            current_pages = data["current_page"]
            cases = Menu.objects.filter(pid=pid)
            case_total = len(cases)
            result = []
            case_list = []
            for case in cases:
                if case.testcase_id is not None and case.testcase_id != '':
                    menus = Menu.objects.filter(id=pid)
                    for menu in menus:
                        case_dict = {
                            "project": menu.label
                        }
                        testcases = Testcase.objects.filter(id=case.testcase_id)
                        for testcase in testcases:
                            testcase_dict = {
                                # 测试用例列表
                                "id": testcase.id,
                                "name": testcase.t_name,
                                "method": testcase.t_method,
                                "url": testcase.t_url,
                                "environment": testcase.t_environment.e_address,
                                "update_time": datetime.strftime(testcase.update_time, "%Y-%m-%d %H:%M:%S"),
                                "create_time": datetime.strftime(testcase.create_time, "%Y-%m-%d %H:%M:%S"),
                                "username": testcase.t_user.username
                            }
                            case_dict.update(testcase_dict)
                        case_list.append(case_dict)
            result.extend(case_list)
            p = Paginator(result, size_page)
            page1 = p.page(current_pages)
            current_page = page1.object_list
            return JsonResponse({
                "code": 200,
                "message": "查询用例列表成功",
                "content": current_page,
                "case_total": case_total,
                "size_page": size_page,
                "current_page": current_pages,
                "id": data["id"]
            })
        elif path == "/api/testcase/all_list":
            # 获取全部接口
            cases = Testcase.objects.all()
            case_all = []
            for case in cases:
                caseslist = []
                casedict = {
                    # 测试用例列表
                    "id": case.id,
                    "name": case.t_name,
                    "method": case.t_method,
                    "url": case.t_url,
                    "environment": case.t_environment.e_address,
                    "update_time": datetime.strftime(case.update_time, "%Y-%m-%d %H:%M:%S"),
                    "create_time": datetime.strftime(case.create_time, "%Y-%m-%d %H:%M:%S"),
                    "username": case.t_user.username
                }
                menus = Menu.objects.filter(testcase_id=case.id)
                for menu in menus:
                    pid = menu.pid
                    labels = Menu.objects.filter(id=pid)
                    for label in labels:
                        menudict = {
                            "project": label.label
                        }
                        casedict.update(menudict)
                    caseslist.append(casedict)
                case_all.extend(caseslist)
            return JsonResponse({
                "code": 200,
                "message": "查询所有用例列表成功",
                "content": case_all
            })
